import os


def gcfnpdap(file_path, n):
    """
        function description: get current file n grade parent directory absolute path
        args：
            file_path: 文件绝对路径
            n：获得当前文件的几级父目录绝对路径，1表示当前文件直接父目录绝对路径，2级表示获得当前文件直接父目录的直接父目录绝对路径，以此类推
    """

    current_file_path = os.path.abspath(file_path)
    ancestor_absolute_path = os.path.dirname(current_file_path)
    split_path = ancestor_absolute_path.split("\\")

    # 获得当前执行文件n级父目录绝对路径
    dir_absolute_path = "/".join(split_path[:(len(split_path)-n+1)])

    return dir_absolute_path


def filter_docs(list_, value_):
    """
    desc: 过滤相似性较低的语料
    :param list_: 向量数据库查询的相似的docs
    :param value_: 自定义相似性阀值
    :return:
    """
    return [i for i in list_ if i[1] >= value_]


def measure_chat_history_length(chat_history):
    """判断字符列表中所有元素总长度"""
    total_length = 0
    for i in chat_history:
        total_length = total_length + len(i)

    return total_length


def test_chat_history_length(chat_history, max_chat_length):
    """检验字符列表总长度，如果长度过长，删去历史记录"""
    if measure_chat_history_length(chat_history) <= max_chat_length:
        return chat_history

    else:
        chat_history.pop(1)
        chat_history.pop(1)

        return test_chat_history_length()


__all__ = ["gcfnpdap", "filter_docs", "test_chat_history_length"]



